--[[
Licenced under GNU GPL.
For more information, see LICENCE in the main folder.
--]]

--[[
Contains computational utilities.

@version	0.1 23 Aug 2011
@author		rodmp
--]]
Computations = {
}

function Computations:new(o)
    o = o or {}
    setmetatable(o, self)
    self.__index = self

    return o
end

function Computations:getDistance(x1, y1, x2, y2)
    return math.floor(math.sqrt(((x1-x2)^2) + ((y1-y2)^2)))
end

function Computations:getDistanceFrom(id1, id2)
    local x1, y1 = GetV(V_POSITION, id1)
    local x2, y2 = GetV(V_POSITION, id2)

    if ((x1 == -1) or (x2 == -1)) then
        return -1
    end

    return self:getDistance(x1, y1, x2, y2)
end

function Computations:isTargetTooFar(id1, id2)
    local x1, y1 = GetV(V_POSITION, id1)
    local x2, y2 = GetV(V_POSITION, id2)
    local distance

    if ((x1 == -1) or (x2 == -1)) then
        return true
    end

    distance = self:getDistance(x1, y1, x2, y2)

    if (distance > 12) then
        return true
    end

    return false
end

function Computations:isCloseToAttack(id1, id2)
    local x1, y1 = GetV(V_POSITION, id1)
    local x2, y2 = GetV(V_POSITION, id2)
    local attackRange = Homun:getAttackRange()
    local distance

    if ((x1 == -1) or (x2 == -1)) then
        return true
    end

    distance = self:getDistance(x1, y1, x2, y2)

    if (attackRange >= distance) then
        return true
    end

    return false
end